/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 11/30/2022
 * Notes: 
 */

#ifndef JSBIND_SCOPE_EXIT_H
#define JSBIND_SCOPE_EXIT_H

#include <functional>

namespace Jsb {
class ScopeExit {
public:
    ScopeExit() = default;

    ScopeExit(const ScopeExit&) = delete;

    void operator=(const ScopeExit&) = delete;

    template<typename F, typename... Args>
    ScopeExit(F&& f, Args&& ... args)
    {
        func_ = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
    }

    ~ScopeExit()
    {
        if (func_) {
            func_();
        }
    }

private:
    std::function<void()> func_;
};
} // namespace Jsb
#define _CONCAT(a, b) a##b
#define _MAKE_SCOPE_(line) ScopeExit _CONCAT(scope, line) = [&]()
#undef SCOPE_GUARD
#define SCOPE_GUARD _MAKE_SCOPE_(__LINE__)
#endif //JSBIND_SCOPE_EXIT_H
